//
//  MTNewClubDetailMemberAvatorCell.swift
//  MT
//
//  Created by ooops on 2020/5/11.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTNewClubDetailMemberAvatorCell: UICollectionViewCell {
    class func reuseIdentity() -> String {
        return "MTNewClubDetailMemberAvatorCellID"
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.clubMemberAvator.kf.cancelDownloadTask()
    }
    
    private lazy var userDefaultIcon = UIImage(named: defaultPlaceholderIcon60x60)
    
    var avator: UIImage? {
        didSet {
            clubMemberAvator.image = nil
            clubMemberAvator.image = avator
        }
    }
    
    func bindDate(user: JMSGUser) {
        
        if let avator = user.avatar {
            self.clubMemberAvator.kf.setImage(with: URL(string: avator), placeholder: self.userDefaultIcon)
        }
        
        self.clubMemberNickNameLabel.text = user.displayName()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    lazy var clubMemberAvator: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.masksToBounds = true
        imageV.layer.cornerRadius = 22
        imageV.layer.masksToBounds = true
        imageV.contentMode = .scaleAspectFill
        imageV.image = UIImage(named: defaultPlaceholderIcon60x60)
        return imageV
    }()
    
    lazy var clubMemberNickNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.textAlignment = .center
        return l
    }()
    
    private func setupUI() {
        contentView.addSubview(clubMemberAvator)
        contentView.addSubview(clubMemberNickNameLabel)
    }
    
    private func makeConstraint() {
        clubMemberAvator.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalTo(44)
        }
        
        clubMemberNickNameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(clubMemberAvator.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(25)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
